<?php


namespace Trawind\Skeleton\Generators\Commands;


use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Console\RequestMakeCommand;
use Illuminate\Support\Collection;
use Prettus\Repository\Generators\FileAlreadyExistsException;
use Prettus\Repository\Generators\Generator;
use Prettus\Repository\Generators\Stub;
use Trawind\Skeleton\Generators\RequestGenerator;

class RequestCommand extends RequestMakeCommand
{
    
    public function __construct(Filesystem $files)
    {
        parent::__construct($files);
        $this->setName('make:skeleton-request');
    }


    /**
     * Execute the command.
     *
     * @see fire()
     * @return void
     */
    public function handle(){
        $this->laravel->call([$this, 'fire'], func_get_args());
    }


    /**
     * Execute the command.
     *
     * @return void
     */
    public function fire()
    {
        try {
            $generators = new Collection();

            $generators->push(new RequestGenerator([
                'name'  => $this->argument('name') . 'CreateRequest'
            ]));

            $generators->push(new RequestGenerator([
                'name'  => $this->argument('name') . 'UpdateRequest'
            ]));

            foreach ($generators as $generator) {
                $generator->run();
            }

            $this->info('Request created successfully.');
        } catch (FileAlreadyExistsException $e) {
            $this->error('Request already exists!');
            return false;
        }
    }
}